package com.example.waimai.contraller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.waimai.common.R;
import com.example.waimai.dto.SetmealDto;
import com.example.waimai.pojo.Category;
import com.example.waimai.pojo.Setmeal;
import com.example.waimai.service.ServiceCategory;
import com.example.waimai.service.ServiceDish;
import com.example.waimai.service.ServiceSetmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealDishContraller {
    @Autowired
    private ServiceDish serviceDish;
    @Autowired
    private ServiceSetmeal serviceSetmeal;
    @Autowired
    private ServiceCategory serviceCategory;


    @GetMapping("/page")//分页查询
    public R<Page> page(int page, int pageSize,String name){
        Page<Setmeal> pageinfo=new Page<>(page,pageSize);
        Page<SetmealDto> pagedto=new Page<>();
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);

        serviceSetmeal.page(pageinfo,lqw);
        //对象拷贝
        BeanUtils.copyProperties(pageinfo,pagedto,"records");
        List<Setmeal> setmeals=pageinfo.getRecords();
         List<SetmealDto> list=setmeals.stream().map((item)->{
          SetmealDto setmealDto=new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);//从item拷贝到setmealDto
            //分类
            Long categoryId = item.getCategoryId();
            //更具分类查id
            Category id = serviceCategory.getById(categoryId);
            if(id!=null){
                String name1 = id.getName();
                setmealDto.setCategoryName(name1);
            }
            return setmealDto;
        }).collect(Collectors.toList());

         pagedto.setRecords(list);
        return R.success(pagedto);
    }


    //增加套餐
    @PostMapping
    @CacheEvict(value = "SetmealCache",allEntries = true)//优化  添加可能出错
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        serviceSetmeal.saveWithDish(setmealDto);
        return R.success("套餐添加成功");
    }
    //删除套餐
    @DeleteMapping
    @CacheEvict(value = "SetmealCache",allEntries = true)//删除所有的缓存数据
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除的员工：{}",ids);
        serviceSetmeal.removeWithDish(ids);//删除套餐和菜品的关联数据
        return R.success("删除成功");
    }

    //修改套餐
    @GetMapping("/{id}")
    public R<SetmealDto> updata1(@PathVariable Long id){
      SetmealDto setmealDto=  serviceSetmeal.getByidWithDish(id);
      return  R.success(setmealDto);
    }

    //提交修改结果
    @PutMapping
    public R<String> updata2(@RequestBody SetmealDto setmealDto){
        serviceSetmeal.updataByidWithDish(setmealDto);
        return R.success("修改成功");
    }


    //根据参数查询套餐情况
    @GetMapping("/list")
    @Cacheable(value = "SetmealCache",key ="#setmeal.categoryId+'-'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = serviceSetmeal.list(lqw);
        return  R.success(list);
    }
}
